class Node:
    def __init__(self):
        self.data = None
        self.left = None
        self.right = None


class Tree:

    def __init__(self, l):
        self.root = None
        if l:
            self.root = self.build(l, 0, len(l) - 1)

    def build(self, l, min, max):
        newNode = Node()
        middle = int((min + max) / 2)
        newNode.data = l[middle]
        if middle > min:
            newNode.left = self.build(l, min, middle - 1)

        if middle < max:
            newNode.right = self.build(l, middle + 1, max)

        return newNode

    def foreach(self, newNode):
        if newNode is None:
            return
        print(newNode.data)

        if newNode.left:
            self.foreach(newNode.left)

        if newNode.right:
            self.foreach(newNode.right)

    def loop(self):
        self.foreach(self.root)


if __name__ == '__main__':
    l = [2, 3, 4, 5, 6, 7, 8]
    b = Tree(l)
    b.loop()
